package com.zerui.manager.deviceTouch.gatewayimpl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zerui.manager.deviceTouch.DeviceTouch;
import com.zerui.manager.deviceTouch.convertor.DeviceTouchConvert;
import com.zerui.manager.deviceTouch.entity.DeviceTouchDO;
import com.zerui.manager.deviceTouch.gateway.DeviceTouchGateWay;
import com.zerui.manager.deviceTouch.mapper.DeviceTouchMapper;
import com.zerui.manager.deviceTouchSync.DeviceTouchSync;
import com.zerui.manager.web.cmd.DeviceTouchQryCmd;
import com.zerui.manager.web.enums.DelFlagEnum;
import com.zerui.manager.web.enums.DeviceFromEnum;
import com.zerui.manager.web.vo.DeviceTouchPageVO;
import com.zerui.manager.web.vo.feign.DeviceInfoFeignVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
public class DeviceTouchGateWayImpl implements DeviceTouchGateWay {

    @Resource
    private DeviceTouchMapper deviceTouchMapper;

    @Override
    public List<DeviceTouch> selectTouchDevicesByFrom(Integer deviceFrom) {
        LambdaQueryWrapper<DeviceTouchDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceTouchDO::getDeviceFrom, deviceFrom);
        List<DeviceTouchDO> deviceTouchDOS = deviceTouchMapper.selectList(queryWrapper);
        return DeviceTouchConvert.INSTANCE.convertDeviceTouchDosToDeviceTouchs(deviceTouchDOS);
    }

    @Override
    public Page<DeviceTouch> getPageList(DeviceTouchQryCmd deviceTouchQryCmd) {
        LambdaQueryWrapper<DeviceTouchDO> queryWrapper = getDeviceTouchDOLambdaQueryWrapper(deviceTouchQryCmd);
        Page<DeviceTouchDO> page = new Page<>(deviceTouchQryCmd.getPageIndex(), deviceTouchQryCmd.getPageSize());
        Page<DeviceTouchDO> deviceTouchDOPage = deviceTouchMapper.selectPage(page, queryWrapper);
        return DeviceTouchConvert.INSTANCE.covertDeviceTouchDOPageToAccessRecord(deviceTouchDOPage);
    }

    @Override
    public Page<DeviceTouchPageVO> getPageList2(DeviceTouchQryCmd deviceTouchQryCmd) {
        Page<DeviceTouchPageVO> page = new Page<>(deviceTouchQryCmd.getPageIndex(), deviceTouchQryCmd.getPageSize());
        return deviceTouchMapper.selectPage2(page, deviceTouchQryCmd);
    }

    @Override
    public void handTouchDevices(List<DeviceTouch> addDevices, List<DeviceTouch> updateDevices, List<DeviceTouch> deleteDevices, DeviceTouchSync touchSync) {
        addDevices.forEach(c -> {
            c.setCreateBy(touchSync.getSyncOperator());
            c.setCreateTime(touchSync.getSyncTime());
            c.setTouchSyncId(touchSync.getId());
            c.setTouchSyncHandle("add"); // 新增
            try {
                DeviceTouchDO deviceTouchDO = DeviceTouchConvert.INSTANCE.convertDeviceTouchToDeviceTouchDo(c);
                deviceTouchMapper.insert(deviceTouchDO);
            } catch (Exception e) {
                log.error("从" + DeviceFromEnum.getLabel(c.getDeviceFrom()) + "【新增门禁设备】出错", e);
            }
        });
        /*为避免出现DeviceId重复导致一批数据不能插入的问题，将批量新增改成上述单条新增
        if (!addDevices.isEmpty()) {
            List<DeviceTouchDO> deviceTouchDOS = DeviceTouchConvert.INSTANCE.convertDeviceTouchsToDeviceTouchDos(addDevices);
            deviceTouchMapper.insertBatchSomeColumn(deviceTouchDOS);
        }*/
        updateDevices.forEach(c -> {
            c.setUpdateBy(touchSync.getSyncOperator());
            c.setUpdateTime(touchSync.getSyncTime());
            c.setTouchSyncId(touchSync.getId());
            c.setDelFlag(DelFlagEnum.FLAG_NORMAL.getCode() + "");
            c.setTouchSyncHandle("update"); // 新增
        });
        if (!updateDevices.isEmpty()) {
            List<DeviceTouchDO> deviceTouchDOS = DeviceTouchConvert.INSTANCE.convertDeviceTouchsToDeviceTouchDos(updateDevices);
            deviceTouchMapper.updateBatch(deviceTouchDOS);
        }
        deleteDevices.forEach(c -> {
            c.setUpdateBy(touchSync.getSyncOperator());
            c.setUpdateTime(touchSync.getSyncTime());
            c.setDelFlag(DelFlagEnum.FLAG_DELETED.getCode() + "");
            c.setTouchSyncId(touchSync.getId());
            c.setTouchSyncHandle("delete"); // 新增
        });
        if (!deleteDevices.isEmpty()) {
            List<DeviceTouchDO> deviceTouchDOS = DeviceTouchConvert.INSTANCE.convertDeviceTouchsToDeviceTouchDos(deleteDevices);
            deviceTouchMapper.updateBatch(deviceTouchDOS);
        }
    }

    @Override
    public void autoTouchDevices(List<DeviceTouch> addDevices, List<DeviceTouch> updateDevices, List<DeviceTouch> deleteDevices) {
        addDevices.forEach(c -> {
            c.setCreateBy("auto");
            c.setCreateTime(LocalDateTime.now());
            c.setTouchSyncHandle("add"); // 新增
            try {
                DeviceTouchDO deviceTouchDO = DeviceTouchConvert.INSTANCE.convertDeviceTouchToDeviceTouchDo(c);
                deviceTouchMapper.insert(deviceTouchDO);
            } catch (Exception e) {
                log.error("从" + DeviceFromEnum.getLabel(c.getDeviceFrom()) + "【新增门禁设备】出错", e);
            }
        });
        /* 由批量保存改成上述单条新增
        if (!addDevices.isEmpty()) {
            List<DeviceTouchDO> deviceTouchDOS = DeviceTouchConvert.INSTANCE.convertDeviceTouchsToDeviceTouchDos(addDevices);
            deviceTouchMapper.insertBatchSomeColumn(deviceTouchDOS);
        }*/
        updateDevices.forEach(c -> {
            c.setUpdateBy("auto");
            c.setUpdateTime(LocalDateTime.now());
            c.setDelFlag(DelFlagEnum.FLAG_NORMAL.getCode() + "");
            c.setTouchSyncHandle("update"); // 新增
        });
        if (!updateDevices.isEmpty()) {
            List<DeviceTouchDO> deviceTouchDOS = DeviceTouchConvert.INSTANCE.convertDeviceTouchsToDeviceTouchDos(updateDevices);
            deviceTouchMapper.updateBatch(deviceTouchDOS);
        }
        deleteDevices.forEach(c -> {
            c.setUpdateBy("auto");
            c.setUpdateTime(LocalDateTime.now());
            c.setDelFlag(DelFlagEnum.FLAG_DELETED.getCode() + "");
            c.setTouchSyncHandle("delete"); // 新增
        });
        if (!deleteDevices.isEmpty()) {
            List<DeviceTouchDO> deviceTouchDOS = DeviceTouchConvert.INSTANCE.convertDeviceTouchsToDeviceTouchDos(deleteDevices);
            deviceTouchMapper.updateBatch(deviceTouchDOS);
        }
    }

    @Override
    public List<DeviceInfoFeignVO> getAccessControlDeviceList(DeviceTouchQryCmd qryCmd) {
        return deviceTouchMapper.selectDeviceList(qryCmd);
    }

    @Override
    public DeviceTouchPageVO getDeviceByDeviceCode(String deviceCode) {
        List<DeviceTouchPageVO> touchPageVOS = deviceTouchMapper.selectDeviceByCode(deviceCode);
        return touchPageVOS.size() > 0 ? touchPageVOS.get(0) : null;
    }

    private LambdaQueryWrapper<DeviceTouchDO> getDeviceTouchDOLambdaQueryWrapper(DeviceTouchQryCmd cmd) {
        LambdaQueryWrapper<DeviceTouchDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(cmd.getProductCode()), DeviceTouchDO::getProductCode, cmd.getProductCode());
        queryWrapper.eq(StringUtils.isNotBlank(cmd.getDeviceCode()), DeviceTouchDO::getDeviceCode, cmd.getDeviceCode());
        queryWrapper.eq(StringUtils.isNotBlank(cmd.getBuildNo()), DeviceTouchDO::getBuildNo, cmd.getBuildNo());
        queryWrapper.eq(StringUtils.isNotBlank(cmd.getLevelNo()), DeviceTouchDO::getLevelNo, cmd.getLevelNo());
        queryWrapper.eq(StringUtils.isNotBlank(cmd.getRoomNo()), DeviceTouchDO::getRoomNo, cmd.getRoomNo());
        queryWrapper.in(cmd.getDeviceIds() != null && !cmd.getDeviceIds().isEmpty(), DeviceTouchDO::getDeviceId, cmd.getDeviceId());
        queryWrapper.like(StringUtils.isNotBlank(cmd.getDeviceName()), DeviceTouchDO::getDeviceName, cmd.getDeviceName());
        queryWrapper.likeRight(StringUtils.isNotBlank(cmd.getPosition()), DeviceTouchDO::getPosition, cmd.getPosition());
        return queryWrapper;
    }
}
